from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from .process_time_header import add_process_time_header


def register_middleware(app: FastAPI) -> None:
    # 跨域配置
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
        allow_headers=["*"],
    )
    # 请求时间统计
    app.middleware("http")(add_process_time_header)

